//
//  AppDelegate.swift
//  微博模拟项目
//
//  Created by 尹家鑫 on 16/3/12.
//  Copyright © 2016年 尹家鑫. All rights reserved.
//
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //设置全局颜色
        setThemeColor()
        
        //注册通知
        registerNotification()
        
        //需要手动实例化window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        
        //设置根视图控制器
        defaultRootViewController()
        
        //可视化window
        window?.makeKeyAndVisible()
        
        return true
    }
    
    //通知的监听方法.一般会带一个通知对象
    @objc private func changeRootVC(n: NSNotification) {
        window?.rootViewController = (n.object == nil) ? MainViewController() : WelcomeViewController()
    }
    
    
    //监听的方法没有实现
    private func registerNotification() {
        //注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeRootVC:", name: SwitchRootViewController, object: nil)
    }
    
    //判断用户是否登录.用户登录显示欢迎页面.用户未登录显示访客视图
    private func defaultRootViewController() {
        let rootVC = UserAccount.shareAccount.userLogin ? WelcomeViewController() : MainViewController()
        window?.rootViewController = rootVC
    }
    
    private func setThemeColor() {
        //设置导航条的主题色
        UINavigationBar.appearance().tintColor = themeColor
        UITabBar.appearance().tintColor = themeColor
    }
    
    //移除通知
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
}


